from flask import render_template, session, request, jsonify, redirect
from werkzeug.security import generate_password_hash

from views import user_blue
from models import db
from models.index import User, Commodity_classify_one, Order_form
from utils.index import if_user, date, order_count_list


# 用户界面
@user_blue.route('/user')
def user():
    # 获取用户对象，没登陆直接跳转主页
    user = if_user()
    if not user:
        return redirect('/registered/login')
    # print('--------------------------', type(user))
    # 获取分类信息
    classify_one = db.session.query(Commodity_classify_one).all()

    # 获取用户的订单信息
    user_order_list, order_price_count = order_count_list()

    return render_template('User.html', user=user, classify_one=classify_one,
                           user_order_list=user_order_list, order_price_count=order_price_count)


# 修改密码页面
@user_blue.route('/user/changaPassword')
def changapassword():
    # 判断是否登录
    user = if_user()
    if not user:
        return redirect('/registered/login')
    classify_one = db.session.query(Commodity_classify_one).all()
    return render_template('User_changePassword.html', user=user, classify_one=classify_one)


# 修改密码逻辑
@user_blue.route('/user/changaPassword_from', methods=['POST'])
def changapassword_from():
    # 获取原密码 新密码 再次输入密码
    old_password = request.json.get("old_password")
    new_password = request.json.get("new_password")
    again_password = request.json.get("again_password")

    # 获取用户信息  没登陆跳转到登录页面
    user = if_user()
    if not user:
        return redirect('/registered/login')

    # 测试数据
    print('--------------------------')
    print(old_password, new_password, again_password)
    print('--------------------------')

    # 判断表格是否没填写完成
    if not (old_password and new_password and again_password):
        ret = {
            "errno": 3001,
            "errmsg": "表格填写错误"
        }

    elif old_password == new_password:
        ret = {
            "errno": 3002,
            "errmsg": "新密码和原密码相同"
        }

    # 判断两次输入密码是否相同
    elif new_password != again_password:
        ret = {
            "errno": 3003,
            "errmsg": "两次输入密码不相同"
        }

    # 密码长度不能小于六位
    elif len(new_password) < 6:
        ret = {
            "errno": 3004,
            "errmsg": "密码长度小于六位"
        }

    # 修改成功
    else:
        ret = {
            "errno": 3000,
            "errmsg": "修改成功"
        }

        # 将用户数据修改
        user.password = generate_password_hash(new_password)
        db.session.commit()

        # 消除缓存 返回到登录页面 重新登录
        session.clear()

    return jsonify(ret)


# 个人信息页面
@user_blue.route('/user/Personalinfo')
def personalinfo():
    user = if_user()
    if not user:
        return redirect('/registered/login')
    classify_one = db.session.query(Commodity_classify_one).all()
    return render_template('User_Personalinfo.html', user=user, date=date, classify_one=classify_one)


# 个人信息逻辑
@user_blue.route('/user/Personalinfo_from', methods=['POST'])
def personalinfo_from():
    # 获取用户对象
    user = if_user()

    # 获取个人信息表中的数据
    year = request.json.get("year")
    month = request.json.get("month")
    day = request.json.get("day")
    sex = request.json.get("sex")
    e_mail = request.json.get("e_mail")
    QQ = request.json.get("QQ")
    mobile_num = request.json.get("mobile")
    nick_name = request.json.get("nick_name")
    print('========================================================')
    print(year, month, day, '----', sex, '------', e_mail, QQ, mobile_num, nick_name)
    print('========================================================')

    if len(e_mail) > 32:
        ret = {
            "errno": 4001,
            "errmsg": "邮箱输入错误"
        }

    elif len(mobile_num) != 11 and mobile_num.isdigit():
        ret = {
            "errno": 4002,
            "errmsg": "电话号码错误"
        }

    elif len(QQ) > 16:
        ret = {
            "errno": 4003,
            "errmsg": "QQ输入错误"
        }

    elif len(nick_name) > 64:
        ret = {
            "errno": 4004,
            "errmsg": "昵称过长"
        }
    else:
        # 将数据添加到数据库
        user.birthday = year + month + day
        user.gender = sex
        user.e_mail = e_mail
        user.QQ = QQ
        user.mobile_num = mobile_num
        user.nick_name = nick_name
        db.session.commit()
        ret = {
            "errno": 4000,
            "errmsg": "修改成功"
        }

    return jsonify(ret)


# 订单管理页面
@user_blue.route('/user/user_orderform')
def user_order_form():
    # 获取用户对象
    user = if_user()
    if not user:
        return redirect('/registered/login')

    # user_order_list为订单大列表，order_price_count对应下标的订单物品总金额
    user_order_list, order_price_count = order_count_list()
    classify_one = db.session.query(Commodity_classify_one).all()
    return render_template('User_Orderform.html', user=user, user_order_list=user_order_list,
                           order_price_count=order_price_count, classify_one=classify_one)

